﻿Imports System.Data.OleDb
Public Class frmThemSanPham
#Region "Form Event"
    Public IDSanPham As Integer = -1
    Private Sub frmThemSanPham_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            LoaiSanPham_Load()
            DonViTinh_Load()
            txtMaLoai.BackColor = Color.White
            cbxTheoDoiTonKho.Checked = True
            'QuyCach_Load(cbxDonViTinh.SelectedValue)
            If IDSanPham <> -1 Then
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dsSanPham As New DataSet
                Dim dtSanPham As New DataTable
                Dim query As String = "Select * From SanPham Where IDSanPham=" & IDSanPham
                Dim adapter As OleDb.OleDbDataAdapter
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dsSanPham)
                dbConn.Close()
                dtSanPham = dsSanPham.Tables(0)
                cbxLoaiSanPham.SelectedValue = dtSanPham.Rows(0)("IdLoai")
                txtMaLoai.Text = dtSanPham.Rows(0)("MaSanPham")
                txtSanPham.Text = dtSanPham.Rows(0)("TenSanPham")
                cbxDonViTinh.SelectedValue = dtSanPham.Rows(0)("DVTSanPhamId")
                txtGhiChu.Text = IIf(IsDBNull(dtSanPham.Rows(0)("GhiChu")), "", dtSanPham.Rows(0)("GhiChu"))
                'Theo Doi So Luong Ton
                cbxTheoDoiTonKho.Checked = IIf(dtSanPham.Rows(0)("TheoDoiTon") = 1, True, False)
            End If
            'lblChuThich
            ThongTinDonViTinh()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"
    Private Sub txtMaLoai_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMaLoai.KeyUp
        Try
            txtMaLoai.BackColor = Color.White
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtSanPham_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSanPham.KeyUp
        Try
            txtSanPham.BackColor = Color.White
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If cbxLoaiSanPham.Items.Count - 1 < 0 Then
                MessageBox.Show("Loại sản phẩm chưa có.Vui lòng kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If cbxDonViTinh.Items.Count - 1 < 0 Then
                MessageBox.Show("Đơn vị tính chưa có.Vui lòng kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If txtMaLoai.Text = "" Then
                EmptyData(txtMaLoai)
                Exit Sub
            End If
            If txtSanPham.Text = "" Then
                EmptyData(txtSanPham)
                Exit Sub
            End If
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            If IDSanPham = -1 Then
                'kiểm tra 
                'Kiểm Tra Mã Trùng
                dbCommand = New OleDbCommand("spSanPham_KiemMaTrung", dbConn)
                dbCommand.CommandType = CommandType.StoredProcedure
                dbCommand.Parameters.Add("@MaSanPham", OleDbType.VarWChar).Value = txtMaLoai.Text
                If dbCommand.ExecuteScalar() = 1 Then
                    MessageBox.Show("Mã sản phẩm này đã có trong danh mục.Vui lòng kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    EmptyData(txtMaLoai)
                    Exit Sub
                Else
                    query = String.Format("Insert Into SanPham(IDLoai,MaSanPham,TenSanPham,DVTSanPhamId,GhiChu,NgayTao,NguoiTao,TheoDoiTon) Values({0},N'{1}',N'{2}',{3},N'{4}','{5}','{6}',{7})", cbxLoaiSanPham.SelectedValue, txtMaLoai.Text, txtSanPham.Text.Trim(), cbxDonViTinh.SelectedValue, txtGhiChu.Text, Format(Now, "dd/MM/yyy HH:mm:ss"), frmMain.UserName, IIf(cbxTheoDoiTonKho.Checked, 1, 0))
                End If
            Else
                query = String.Format("Update SanPham Set IDLoai={0},MaSanPham=N'{1}',TenSanPham=N'{2}',DVTSanPhamId={3},GhiChu=N'{4}',NgaySuaGanNhat='{5}',NguoiSuaGanNhat='{6}',TheoDoiTon={7} Where IDSanPham={8}", cbxLoaiSanPham.SelectedValue, txtMaLoai.Text, txtSanPham.Text.Trim(), cbxDonViTinh.SelectedValue, txtGhiChu.Text, Format(Now, "dd/MM/yyy HH:mm:ss"), frmMain.UserName, IIf(cbxTheoDoiTonKho.Checked, 1, 0), IDSanPham)
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtMaLoai.Text = ""
            txtSanPham.Text = ""
            txtGhiChu.Text = ""
            txtMaLoai.BackColor = Color.White
            QuanLyKhachHang.frmHopDong.AutoComplete()
            If IDSanPham <> -1 Then
                Me.Close()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtMaLoai.Text = ""
            txtSanPham.Text = ""
            txtGhiChu.Text = ""
            txtMaLoai.BackColor = Color.White
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub cbxDonViTinh_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxDonViTinh.SelectionChangeCommitted
        Try
            ThongTinDonViTinh()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Load Data"
    Private Sub LoaiSanPham_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsLoaiSanPham As New DataSet
            Dim dtLoaiSanPham As New DataTable
            Dim query As String = "Select * From LoaiSanPham Order By TenLoai ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsLoaiSanPham)
            dbConn.Close()
            dtLoaiSanPham = dsLoaiSanPham.Tables(0)
            cbxLoaiSanPham.DataSource = dtLoaiSanPham
            cbxLoaiSanPham.ValueMember = "IdLoai"
            cbxLoaiSanPham.DisplayMember = "TenLoai"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub DonViTinh_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtDVTSanPham As New DataTable
            Dim query As String = "Select TenDonViTinh,DVTSanPhamId From DVTSanPham Where Xoa=0 Order By TenDonViTinh"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtDVTSanPham)
            dbConn.Close()
            cbxDonViTinh.DataSource = dtDVTSanPham
            cbxDonViTinh.DisplayMember = "TenDonViTinh"
            cbxDonViTinh.ValueMember = "DVTSanPhamId"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub ThongTinDonViTinh()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtThongTinDVTSanPham As New DataTable
            Dim query As String = "Select DVTSanPham.SoLuong,DonViTinh.TenDonVi,QuyCachDongGoi.TenQuyCachDongGoi from DVTSanPham" & _
                                    " Left Outer Join DonViTinh On DonViTinh.IDDonVi=DVTSanPham.IDDonViTinh" & _
                                    "   Left Outer Join QuyCachDongGoi On QuyCachDongGoi.QuyCachDongGoiId= DVTSanPham.QuyCachDongGoiId" & _
                                    " Where DVTSanPhamId=" & cbxDonViTinh.SelectedValue
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtThongTinDVTSanPham)
            dbConn.Close()
            lblChuThich.Text = " Chú giải : " & dtThongTinDVTSanPham.Rows(0)("SoLuong") & " " & dtThongTinDVTSanPham.Rows(0)("TenDonVi") & " / " & dtThongTinDVTSanPham.Rows(0)("TenQuyCachDongGoi")
        Catch ex As Exception

        End Try
    End Sub
#End Region

    
End Class